'''
Created on 2010/07/09

@author: saburo
'''

from pyrfid.Util.Util import Util
from pyrfid.DataProtocol.LogicalMemory.DataSet import DataSet
from pyrfid.DataProtocol.DSFID.Processor.Compactor import Compactor
import sys


class DataFormatUtil(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        pass
        
    @staticmethod
    def convertDataSet(add_objects_list):
        add_data_set_list = list()
        ########################################################################
        #reference 15962 Annex G4
        #compaction
        for add_object in add_objects_list:
            data_set = DataSet()
            
            result = Compactor.compactObject(add_object.OBJECT, int(add_object.COMPACT_PARAMETER))            
            
            data_set.LENGTH_OF_OBJECT = result['lengthOfObject']
            data_set.OBJECT = result['object'].upper()            
            data_set.PRECURSOR = result['precursor']
            data_set.OBJECT_IDENTIFIER = add_object.OBJECT_IDENTIFIER
            add_data_set_list.append(data_set)
            
        return add_data_set_list
    
    @staticmethod
    def encodeLengthOfIdentifier(data_set_list):
        
        for data_set in data_set_list:
            
            if len(data_set.OBJECT_IDENTIFIER) == 2:
                if int(data_set.OBJECT_IDENTIFIER, 16) < int("0E", 16):
                    data_set.PRECURSOR = data_set.PRECURSOR[:4] + Util.convertHexToBit(data_set.OBJECT_IDENTIFIER[1]) 
                    data_set.OBJECT_IDENTIFIER = ""
                    continue
            
            data_set.PRECURSOR = data_set.PRECURSOR[:4] + '1111'
            length_of_object_identifier = len(data_set.OBJECT_IDENTIFIER) / 2
            if length_of_object_identifier >= 1 and length_of_object_identifier <= 16:
                lenHex = Util.convertBitToHex('100' + Util.convertIntToBit(length_of_object_identifier + 1).zfill(5))
                data_set.LENGTH_OF_OBJECT_IDENTIFIER = lenHex
            elif length_of_object_identifier >= 17 and length_of_object_identifier <= 126:
                lenHex = "A0" + Util.convertIntToHex(length_of_object_identifier, 2)
                data_set.LENGTH_OF_OBJECT_IDENTIFIER  = lenHex
        
        return data_set_list

    
    @staticmethod
    def isOffset(precursor):
        bit_precursor = Util.convertHexToBit(precursor)
        if bit_precursor[0] != "1":
            return False
        return True

    @staticmethod    
    def isPrecursor(precursor):
        if precursor == "80":
            return False
        return True
    
    @staticmethod
    def isPrecursorHaveAllObjectIdentifier(precursor):
        binary_precursor = Util.convertHexToBit(precursor)        
        if binary_precursor[4:] == "1111":
            return False
        return True

    @staticmethod
    def isLengthOfObjectIdentifier(length_of_object_identifier):
        '''
        length_of_object_identifier -- a string data
        '''
        for index in range(len(length_of_object_identifier) / 2):
            bit_length_of_object_identifier = Util.convertHexToBit(length_of_object_identifier[index*2:index*2+2])
            print bit_length_of_object_identifier
#            if bit_length_of_object_identifier[:3] != "100":
#                raise "Not Related OID"
            if bit_length_of_object_identifier[3:] == "11111":
                return False
            
        return True   
   
    @staticmethod
    def isLengthOfObject(length_of_object):
        if len(length_of_object) == 2:
            binary_length_of_object = Util.convertHexToBit(length_of_object)
            if binary_length_of_object[0] == "0":
                return True
            return False
        
        for index in range(len(length_of_object) / 2):
            binary_length_of_object = Util.convertHexToBit(length_of_object[index*2:index*2+2])
            if binary_length_of_object[0] == "0":
                return True

        return False

    @staticmethod
    def decodeLengthOfObject(length_of_object):
        length = 0
        for index in range(len(length_of_object) / 2):
            bin =  Util.convertHexToBit(length_of_object[index * 2: index * 2 + 2])
            int_bin = int(bin, 2)
            shifted_bin = int_bin << index * 7
            length += shifted_bin
        return length
    
    
    @staticmethod
    def decodeLengthOfObjectIdentifierType(length_object_identifier_type):        
        bit = Util.convertHexToBit(length_object_identifier_type)        
        print bit
        if bit[0:3] != "100" and bit[0:3] != "101":
            raise "error"
        
        if bit[0:3] == "100":
            return int(bit[3:], 2) - 1
        #ref 101
        return int(bit[9:], 2)
        

